//
//  AddSelfAddressViewController.m
//  shop
//
//  Created by 邵存将 on 17/5/31.
//  Copyright © 2017年 geek-zoo studio. All rights reserved.
//
#import "AddressDao.h"
#import <Masonry/Masonry.h>
#import "AddSelfAddressViewController.h"
#import <SVPullToRefresh/SVPullToRefresh.h>
#import "AddAddressTableViewCell.h"
#import "SVProgressHUD.h"

@interface AddSelfAddressViewController ()

@end

@implementation AddSelfAddressViewController{
    AddressDao *addressDao;
    BOOL _isLoadingData;
    NSMutableArray *datas;
    NSDictionary *currentdata;
}


-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.address_id) {
        self.title=@"编辑自提站点";
    }else{
        self.title=@"新建自提站点";
    }
    [addBtn setTitle:self.title forState:UIControlStateNormal];
    addressDao=[[AddressDao alloc]initWithDelagate:self];
    datas=[[NSMutableArray alloc]init];
//    _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//    [self.view addSubview:_tableView];
    _tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self setExtraCellLineHidden:_tableView];
    datas=[[NSMutableArray alloc]init];
//    _tableView.contentInset=UIEdgeInsetsMake(64, 0, 49, 0);
//    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
//    }];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor=[UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator=NO;
    [_tableView addPullToRefreshWithActionHandler:^{
        if (!_isLoadingData) {[self reloadData];}
    }];
    [_tableView.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateAll];
    [_tableView.pullToRefreshView setTitle:@"松开刷新" forState:SVPullToRefreshStateTriggered];
    [_tableView.pullToRefreshView setTitle:@"正在加载" forState:SVPullToRefreshStateLoading];
    [_tableView registerNib:[UINib nibWithNibName:@"AddAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"AddAddressTableViewCell"];
    [self reloadData];
        showView.hidden=YES;
    coverView.hidden=YES;
    [self showBarButton:BeeUINavigationBar.LEFT image:[UIImage imageNamed:@"item_grid_header_arrow_icon.png"]];
    
}



-(void)reloadData{
    [addressDao get_pickup_point];
}

- (void)dealloc
{
    addressDao.remoteDaoDelegate=nil;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=datas[indexPath.row];
    AddAddressTableViewCell *addressCell= (AddAddressTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AddAddressTableViewCell"];
    addressCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [addressCell setContent:dict];
    return addressCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    showView.hidden=NO;
    coverView.hidden=NO;
    NSDictionary *dict=datas[indexPath.row];
    currentdata=dict;
    name.text=[dict objectForKey:@"name"];
    address.text=[dict objectForKey:@"address"];
}



-(void)getRemotedata:(NSMutableDictionary*)dic{
    if ([dic objectForKey:@"get_pickup_point"]) {
        [_tableView.pullToRefreshView stopAnimating];
        [datas removeAllObjects];
        NSArray  *addressdatas=[[dic objectForKey:@"get_pickup_point"] objectForKey:@"info"];
        [datas addObjectsFromArray:addressdatas];
        [_tableView reloadData];
        
    }
    if ([dic objectForKey:@"add_my_address"]||[dic objectForKey:@"edit_my_address"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }

    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)close:(id)sender{
    showView.hidden=YES;
    coverView.hidden=YES;
}

-(IBAction)save:(id)sender{
    if (tel.text.length==0) {
        [SVProgressHUD showErrorWithStatus:@"电话不能为空"];
        return;
    }
    if (self.address_id) {
        [addressDao edit_my_address:[[currentdata objectForKey:@"id"]integerValue] tel:tel.text address_id:[self.address_id integerValue]];
    }else{
         [addressDao add_my_address:[[currentdata objectForKey:@"id"]integerValue] tel:tel.text];
    }
   
}

ON_LEFT_BUTTON_TOUCHED( signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
